package com.xuexiang.xui_lib.component.textview.marqueen;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageFlipper;
import ohos.app.Context;

import java.util.List;

/**
 * 文字轮播控件【类似字幕、头条之类】
 *
 * @author xuexiang
 * @since 2019/1/14 下午10:03
 */
public class MarqueeView extends PageFlipper {
    /**
     * Item翻页时间间隔【mInterval 必须大于 mAnimDuration，否则视图将会重叠】
     */
    private int mInterval = 2500;
    /**
     * Item动画执行时间
     */
    //private int mAnimDuration = 500;
    /**
     * 进出动画
     */
    private AnimatorProperty mAnimIn, mAnimOut;

   /* public MarqueeView(Context context) {
        super(context);
        init(null);
    }*/

    public MarqueeView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttrSet attrSet) {
        if (attrSet.getAttr("mq_interval").isPresent()) {
            mInterval = attrSet.getAttr("mq_interval").get().getIntegerValue();
        }
        if (attrSet.getAttr("mq_animDuration").isPresent()) {
            //mAnimDuration = attrSet.getAttr("mq_animDuration").get().getIntegerValue();
        }

        mAnimIn = new AnimatorProperty();
        mAnimIn.moveFromY(getHeight()).moveToY(0).alphaFrom(0).alpha(1).setDuration(500);

        mAnimOut = new AnimatorProperty();
        mAnimOut.moveFromY(0).moveToY(-getHeight()).alphaFrom(1).alpha(0).setDuration(500);
        setIncomingAnimation(mAnimIn);
        setOutgoingAnimation(mAnimOut);

        setFlipPeriod(mInterval);
    }

    private void measureChildren(int widthEstimatedConfig, int heightEstimatedConfig) {
        for (int idx = 0; idx < getChildCount(); idx++) {
            Component childView = getComponentAt(idx);
            if (childView != null) {
                measureChild(childView, widthEstimatedConfig, heightEstimatedConfig);
            }
        }
    }

    private void measureChild(Component child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        ComponentContainer.LayoutConfig lc = child.getLayoutConfig();
        int childWidthMeasureSpec = EstimateSpec.getChildSizeWithMode(
            lc.width, parentWidthMeasureSpec, EstimateSpec.UNCONSTRAINT);
        int childHeightMeasureSpec = EstimateSpec.getChildSizeWithMode(
            lc.height, parentHeightMeasureSpec, EstimateSpec.UNCONSTRAINT);
        child.estimateSize(childWidthMeasureSpec, childHeightMeasureSpec);
    }

    /**
     * 设置滚动数据的工厂
     *
     * @param factory
     */
    public void setMarqueeFactory(MarqueeFactory factory) {
        factory.setAttachedToMarqueeView(this);
        removeAllComponents();
        List<Component> mViews = factory.getMarqueeViews();
        if (mViews != null) {
            for (int i = 0; i < mViews.size(); i++) {
                addComponent(mViews.get(i));
            }
        }
    }

    /**
     * 设置翻页时间间隔
     *
     * @param interval
     */
    public void setInterval(int interval) {
        mInterval = interval;
        setFlipPeriod(interval);
    }

    /**
     * 设置动画执行时间
     *
     * @param animDuration
     */
    public void setAnimDuration(int animDuration) {
        //mAnimDuration = animDuration;
        mAnimIn.setDuration(animDuration);
        setIncomingAnimation(mAnimIn);
        mAnimOut.setDuration(animDuration);
        setOutgoingAnimation(mAnimOut);
    }

    public void setAnimInAndOut(AnimatorProperty animIn, AnimatorProperty animOut) {
        mAnimIn = animIn;
        mAnimOut = animOut;

        setIncomingAnimation(mAnimIn);
        setOutgoingAnimation(mAnimOut);
    }

    public AnimatorProperty getAnimIn() {
        return mAnimIn;
    }

    public AnimatorProperty getAnimOut() {
        return mAnimOut;
    }
}
